//yw 1初始化
layui.use(['laytpl','form','layer','table','element'],function(laytpl){
    var table= layui.table;
    var tBuildID = null;//yw 学校数据用于填充学校下拉框，防止反复从服务器下载学校数据
    var schoolId = null;
    var data = null;
    var data1 = null;
    var data2 = null;
    var data3 = null;
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;

        //添加教室
        $(".add_btn").click(function(){
            mainIndex = layer.open({
                title : ['添加学校','font-size:18px;text-align:center;color:blue;'],
                type : 1, //yw 0.默认信息框,1.page,2.iframe,3.加载层,4.tips层
                content : $("#addDiv"),//xql 调用弹出框
                area:['350px','520px'],
                offset:'r'	//xql 弹出框的位置
            });
        });

        //yw 3操作 2)新增 (b)提交监听事件--新增记录
        form.on("submit(doSubmitAdd)", function () {
            $.ajax({
                type: "post",
                url: "/classroom/addClassRoom",
                data: {
                    classroomid: $("#classroomid").val(),
                    tbuildid: $("#tbuildid").val(),
                    classroom: $("#classroom").val(),
                    addtime: $("#addtime").val()
                },
                success: function (res) {
                    if (res) {
                        tableIns.reload();
                        layer.msg('新增成功', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                    } else {
                        layer.msg('新增失败', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                    }
                    layer.closeAll('page'); //yw 关闭新增界面
                    tableIns.close;
                }
            });
        });

        $.post({
            url: "/school/getAllSchoolName",
            async: false,
            success: function (res) {
                // console.log(res);
                data3 = res;
            }
        });
        //建立视图。用于呈现渲染结果。
        var gettp3 = document.getElementById('testTp3').innerHTML;
        laytpl(gettp3).render(data3, function (html) {
            // console.log(html)
            //得到的模板渲染到html
            document.getElementById('test1').innerHTML = html;
        });
        // var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        form.render();
        form.render('select' ,'test'); //刷新select选择框渲染

        form.on('select(mySelect1)', function (data) {
            schoolId = data.value;

            $.post({
                url: "/build/queryAllBuild?schoolid=" + schoolId,
                async: false,
                success: function (res) {
                    // console.log(res);
                    data2 = res;
                }
            });
            //建立视图。用于呈现渲染结果。
            var gettp2 = document.getElementById('testTp2').innerHTML;
            laytpl(gettp2).render(data2, function (html) {
                // console.log(html)
                //得到的模板渲染到html
                document.getElementById('test2').innerHTML = html;
            });

            form.on('select(mySelect2)', function (data) {
                tBuildID = data.value;

                $.post({
                    url: "/classroom/queryAllClassRoom?buildID=" + tBuildID,
                    async: false,
                    success: function (res) {
                        // console.log(res);
                        data1 = res;
                    }
                });

                var gettpl = document.getElementById('testTpl').innerHTML;
                laytpl(gettpl).render(data1, function (html) {
                    // console.log(html)
                    //得到的模板渲染到html
                    document.getElementById('course').innerHTML = html;
                });
                table.render({
                    elem: '#demo'
                    , url: '/classroom/queryAllClassRoom?buildID=' + tBuildID  //数据接口
                    , width: 800
                    , method: 'post' //yw 默认是get方式
                    , cols: [[
                        {field: 'classroomid', title: '教室ID', width: 150, align: 'center'},
                        {field: 'classroom', title: '教室名称', width: 150, align: 'center'},
                        {field: 'tbuildname', title: '教学楼', width: 250, align: 'center'},
                        {field: 'classroomaddtime', title: '添加时间', width: 350, align: 'center'},
                        {fixed: "right", title: '操作', toolbar: '#listRowBar', align: "center"}
                    ]]
                });
            });
        });

            $.post({
            url: "/classroom/queryAllClassRoom?buildID=" + tBuildID,
            async: false,
            success: function (res) {
                // console.log(res);
                data = res;
            }
        });

            // var laytpl = layui.laytpl;
            //建立视图。用于呈现渲染结果。
            var gettpl = document.getElementById('testTpl').innerHTML;
            laytpl(gettpl).render(data, function (html) {
                // console.log(html)
                //得到的模板渲染到html
                document.getElementById('course').innerHTML = html;
            });
        //yw 2显示列表
        var tableIns = table.render({
            elem: '#list',
            url: '/classroom/queryAllClassRoom?buildID=' + tBuildID,
            cellMinWidth: 95,
            page: true,
            width: 1000,
            height: 670,
            limit: 15,
            limits: [15, 10, 20, 30],
            id: "listTable",
            method: 'get',
            cols: [[
                {field: 'classroomid', title: '教室ID', width: 150, align: 'center'},
                {field: 'classroom', title: '教室名称', width: 150, align: 'center'},
                {field: 'tbuildname', title: '教学楼', width: 200, align: 'center'},
                {field: 'classroomaddtime', title: '添加时间', width: 250, align: 'center'},
                {fixed: "right", title: '操作', toolbar: '#listRowBar', align: "center"}
            ]]
        });

        //yw 3操作 2)新增 (a)打开添加的弹出层


        //yw 监听行工具条的事件
        table.on("tool(list)", function (obj) {
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'edit':
                    openUpdateLayer(data);
                    break;
                case 'del':
                    deleteInfo(data);
                    break;
            }
        });


        //yw 3操作 3)更新  (a)打开修改的弹出层
        function openUpdateLayer(data) {
            mainIndex = layer.open({
                title: ['修改教室', 'font-size:18px;text-align:center;color:blue;'],
                type: 1,
                content: $("#saveDiv"),
                area: ['400px', '540px'],
                offset: 'r',
                success:function(){ //yw layero是弹出层对象layer object
                    // alert(data.worderid)
                    $("#classroomid").val(data.classroomid);
                    $("#tbuildid").val(data.tbuildid);
                    //form.render("select");//yw因为后面有form.render("select");
                    $("#classroom").val(data.classroom);
                    $("#addtime").val(data.addtime)
                }
            });
        }

        //yw 3操作 3)更新 (b)提交监听事件--修改记录
        form.on("submit(doSubmitSave)", function () {
            $.ajax({
                type: "post",
                url: "/classroom/save",
                data: {
                    tbuildid: $("#tbuildid2").val(),
                    classroomid: $("#classroomid2").val(),
                    classroom: $("#classroom2").val(),
                    addtime: $("#addtime").val()
                },
                success: function (res) {
                    if (res == 1) {
                        tableIns.reload();
                        layer.msg('保存成功', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                        layer.closeAll('page'); //yw 关闭新增界面 type=1
                    }else if(res == 2) {
                        layer.msg('该教室已经在系统的存在，请更换！', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                    } else {
                        layer.msg('保存失败', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                    }
                }
            });
        });

        //yw 3操作 4)删除
        function deleteInfo(data) {
            layer.confirm('你确定要删除[' + data.classroom + ']这条数据吗?', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    type: "POST",
                    url: '/classroom/defClassRoom=' + data.classroomid,
                    success: function (res) {
                        if (res) {
                            tableIns.reload();
                            layer.msg('删除成功', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                        } else {
                            layer.msg('删除失败', {icon: 7, time: 800, shade: [0.6, '#000', true]});
                        }
                    }
                });
                layer.close(index); //yw 关闭删除提示对话框
            });
        }

});
